package com.fau.cm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCallTelInfo;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 客户电话信息Action
 * @author kangyj
 * @datetime 2014年11月4日,上午12:41:38
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class TelInfoAction extends PublicAction implements
		ModelDriven<MedicalCallTelInfo> {

	private static final long serialVersionUID = -192595574361388905L;
	
	private MedicalCallTelInfo telInfo = new MedicalCallTelInfo();
	
	/**
	 * @desc 打开任务加载电话信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月3日,下午12:12:51
	 */
	@Action(value = "tel_info_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getCallMyManagerService().findTelInfoByCid(telInfo.getCallId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 保存电话信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月4日,上午1:02:18
	 */
	@Action(value = "tel_info_save")
	public String save() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		this.baseAllService.getCallMyManagerService().saveTelInfo(telInfo);
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 编辑电话信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月4日,上午1:01:59
	 */
	@Action(value = "tel_info_edit")
	public String edit() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(telInfo.getId())) {
			this.baseAllService.getCallMyManagerService().editSaveTelInfo(telInfo);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除电话信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月4日,上午1:01:31
	 */
	@Action(value = "tel_info_del")
	public String del() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(telInfo.getId())) {
			this.baseAllService.getCallMyManagerService().delTelInfo(telInfo);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 根据客户id查询客户手机号(用于发送短信)
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月5日,下午3:05:30
	 */
	@Action(value = "customer_tel_find")
	public String findCustomerPhoneNum() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getCallMyManagerService().findCustomerPhoneNums(telInfo.getCustomerId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 修改推荐号码
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月6日,上午11:06:38
	 */
	@Action(value = "customer_recommend_tel_edit")
	public String recommendTelEdit() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		this.baseAllService.getCallMyManagerService().recommendTelEdit(telInfo);
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询客户所有号码
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月6日,下午2:23:00
	 */
	@Action(value = "customer_all_tel_view")
	public String customerAllTelView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getCallMyManagerService().findAllTelInfoByCid(telInfo.getCustomerId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 验证号码是否已存在
	 * @return
	 * @author kangyj
	 * @datetime 2015年2月8日,下午4:03:15
	 */
	@Action(value = "tel_valid_unique")
	public String validNumUnique() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		telInfo.setEid(admin.getEnterpriseId());
		String callId = this.baseAllService.getCallMyManagerService().validNumUnique(telInfo);
		if(StringUtils.isNotBlank(callId)) {
			map.put("success", false);
			map.put("callId", callId);
		} else {
			map.put("success", true);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 客户管理新增客户，添加客户号码
	 * @return String
	 * @author kangyj
	 * @datetime 2015年4月16日,下午6:06:57
	 */
	@Action(value = "tel_info_save_new")
	public String saveNew() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		telInfo.setEid(admin.getEnterpriseId());
		this.baseAllService.getCallMyManagerService().saveNewTelInfo(telInfo);
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 客户管理新增客户，修改客户号码
	 * @return String
	 * @author kangyj
	 * @datetime 2015年4月16日,下午6:08:13
	 */
	@Action(value = "tel_info_edit_new")
	public String editNew() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(telInfo.getId())) {
			this.baseAllService.getCallMyManagerService().editNewTelInfo(telInfo);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 客户管理新增客户，删除客户号码
	 * @return String
	 * @author kangyj
	 * @datetime 2015年4月16日,下午6:09:33
	 */
	@Action(value = "tel_info_del_new")
	public String delNew() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(telInfo.getId())) {
			this.baseAllService.getCallMyManagerService().delNewTelInfo(telInfo);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	@Override
	public MedicalCallTelInfo getModel() {
		return telInfo;
	}

}
